import http from '@/common/http.js'


// 1. 数据进行处理
export const requestDataSortManager = (url, data) => {
	var openId = uni.getStorageSync('openid');
	var clientid = uni.getStorageSync('client_id');
	var access_token = uni.getStorageSync('access_token');

	var newData = {}
	if (data) {
		newData = data
	}
	newData.openid = openId;
	

	return newData
}

// 2. 返回数据进行处理
export const responseDataSortManager = (url, res) => {
	var tempData = {}


	if (res.data) {
		tempData = res.data
	}
	
	var responseInfo = {
		data: tempData,
		success: res.code == 200 ? true : (res.success === true ? true : false),
		errMsg: res.msg ? res.msg  : (res.errMsg ? res.errMsg : '未知异常')
	}


	if (responseInfo.success !== true) {
		
		uni.showToast({
			title: responseInfo.errMsg,
			duration: 2000
		})
	}


	return responseInfo
}



export const GET = (url, data) => {
	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url,
			method: 'get',
			params: newData
		}).then(res => {
			
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POST = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		http.request({
			url: url,
			method: 'post',
			params: newData
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POSTJSON = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		http.request({
			url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/json'
			}
		}).then(res => {

			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POSTFORM = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		http.request({
			url: url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/x-www-form-urlencoded'
			}
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const PUT = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		http.request({
			url: url,
			method: 'put',
			params: newData,
			header: {
				'Content-type': 'application/json'
			}
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const DELETE = (url, data) => {
	return http.request({
		url,
		method: 'delete',
		params: data
	})
}

export const NoLogin = (url, data) => {
	return http.noLogin({
		url,
		method: 'post',
		params: data
	})
}



// post 但是报错不影响主线程
export const POSTJSONNOBACK = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/json'
			}
		}).then(res => {

		})
	})
}


// 请求微信
export const POSTWITHWECHAT = (url, data) => {
	return new Promise(resolve => {
		uni.request({
			url: url,
			success(res) {
				resolve(res);
			},
			fail: function(err) {
				uni.showToast({
					icon: 'none',
					title: '请检查您的网络~~'
				});
			}
		})
	});
}